import { login, updatePassword } from '@/api/user';
import { getToken, setLoginType, setIsLogin, setToken, setUserInfo, getUserInfo, clearLocalStorage } from '@/utils/auth';
import { getLoginType } from '../../utils/auth';

const state = {
  token: getToken(),
  name: '',
  user: ''
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USER: (state, user) => {
    state.user = user
  }
}

const actions = {
  // accountLogin
  accountLogin({ commit }, params) {
    return new Promise((resolve, reject) => {
      login(params).then((responese) => {
        if (responese.code === '20000') {
          commit('SET_TOKEN',responese.data.session)
          
          commit('SET_USER', JSON.stringify(responese.data))
          setLoginType('admin')
          setIsLogin(true)
          setToken(responese.data.session)
          setUserInfo(JSON.stringify(responese.data))
        }
        resolve(responese)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getUserInfo({ commit }) {
    return new Promise((resolve, reject) => {
      
      if (getLoginType() === 'admin') {
        
        commit('SET_NAME',JSON.parse(getUserInfo()).username)
        resolve(JSON.parse(getUserInfo()))
      } else {
        let data = {
          "code": "string",
          "msg": "string",
          "desc": "string",
          "ok": true,
          "data": {
            "email": "string",
            "id": 0,
            "menus": [
              {
                "id": "string",
                "menuId": "string",
                "userId": "string",
                "pageNumber": 0,
                "pageSize": 0
              }
            ],
            "mobile": "string",
            "password": "string",
            "realname": "string",
            "roleId": "string",
            "session": "string",
            "status": 0,
            "username": "string",
            "username2": "string",
            "pageNumber": 0,
            "pageSize": 0
        }
        }
        commit('SET_NAME',data.data.username)
        resolve(data)
      }
    })
    
  },
  // rest password
  resPassword({commit}, query) {
    return new Promise((resolve, reject) => {
      updatePassword(query).then((responese) => {
        if (responese.code === '20000') {
          commit('SET_TOKEN','')
          commit('SET_NAME', '')
          commit('SET_USER', '')
          clearLocalStorage()
        }
        resolve(responese)
      }).catch(error => {
        reject(error)
      })
    })
  },
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      clearLocalStorage()
      commit('SET_TOKEN','')
      commit('SET_NAME', '')
      commit('SET_USER', '')
      let data = {
        code: '20000'
      }
      resolve(data)
    })
  },
  VisitorLogin({commit}) {
    return new Promise((resolve) => {
      commit('SET_TOKEN','visitor')
      setLoginType('visitor')
      setToken('visitor')
      setIsLogin(true)
      resolve({code:'20000'})
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}